//
//  GameLogic.m
//  AnotherFire
//
//  Created by pcwiz on 20/06/09.
//  Copyright 2009 leowiz. All rights reserved.
//

#import "GameLogic.h"


@implementation Side
@synthesize side_name;

-(id) initWithName: (NSString *) _name {
	[super init];
	printf("Initialized a side with name:%s\n", _name);
	self.side_name = _name;
	return self;
}

- (void)dealloc {
    [self.side_name release];
    [super dealloc];
}

@end


@implementation GameLogic

@synthesize sides;
@synthesize board;
@synthesize height;
@synthesize width;
@synthesize cur_side;
@synthesize empty_side;

-(void)dealloc
{
	[sides release];
	[board release];
	[empty_side release];
	[super dealloc];
}

//Initialize the class with the board height and width, and the names of the players:
-(id) initWithGroups: (int)_height width:(int)_width group1:(NSString *)_g1 group2:(NSString *)_g2
{
	[super init];
	printf("Initialized a board with size: %d, %d.\n", _width, _height);

	self.height = _height;
	self.width = _width;
	
	self.empty_side = [[Side alloc] initWithName:@""];
	
	self.sides = [[NSMutableArray alloc] init];
	Side *first = [[Side alloc] initWithName: _g1 ];
	[self.sides addObject: first];
	
	Side *second = [[Side alloc] initWithName: _g2 ];
	[self.sides addObject: second];
	[self startGame];


	return self;
}

-(void) startGame;
{
	self.cur_side = 0;
	self.board = [[NSMutableArray alloc] init];
	
	//Initialize the array with empty sides for each side:
	for (int i = 0; i < self.height; ++i)
	{
		NSMutableArray *a = [[NSMutableArray alloc] init];
		for (int j = 0; j < self.width; ++j)
		{
			[a insertObject: self.empty_side atIndex: 0];
		}
		[self.board addObject: a];
	}
}

//Perform the move for the current player at the given row. Returns the height of the
//move performed or -1 if no move is possibale on that row :
-(int) moveTo:(NSInteger)_where
{
	printf("A move to %d performed by side %d.\n", _where, self.cur_side);
	//Find the lowest place in the row which isn't notified:
	for (int i = 0; i < self.height; ++i)
	{
		NSMutableArray *cur_row = [self.board objectAtIndex: i];
		Side *s = [cur_row objectAtIndex: _where];
		if (self.empty_side == s)
		{
			[cur_row replaceObjectAtIndex: _where withObject: [self.sides objectAtIndex: self.cur_side]];
			self.cur_side = 1 - self.cur_side;
			return i;
		}
	}
	
	//The whole row is apparently full:
	return -1;
}

//Check if someone won (quite stupidly). Return his name or the empty string:
-(NSString*) whoWon
{
	printf("Asking who won.\n");
	Side *winner = self.empty_side;
	
	for (int i = 0; i < self.height; ++i)
	{
		NSMutableArray *cur_row = [self.board objectAtIndex: i];
		
		for (int j = 0; j < self.width; ++j) {
			Side *s = [cur_row objectAtIndex: j];
			
			if (self.empty_side != s)
			{
				@try
				{
					if ( s == [cur_row objectAtIndex:j+1] &&
						 s == [cur_row objectAtIndex:j+2] &&
						 s == [cur_row objectAtIndex:j+3])
					{
						winner = s;
					}
				} @catch (NSException *moo) {}				
				
				@try
				{
					if ( s == [[self.board objectAtIndex:i+1] objectAtIndex:j] &&
						 s == [[self.board objectAtIndex:i+2] objectAtIndex:j] &&
						 s == [[self.board objectAtIndex:i+3] objectAtIndex:j])
					{
						winner = s;						
					}
				} @catch (NSException *moo) {}
				
				@try
				{
					if ( s == [[self.board objectAtIndex:i+1] objectAtIndex:j+1] &&
						 s == [[self.board objectAtIndex:i+2] objectAtIndex:j+2] &&
						 s == [[self.board objectAtIndex:i+3] objectAtIndex:j+3])
					{
						winner = s;						
					}
				} @catch (NSException *moo) {}
				
				@try
				{
					if ( s == [[self.board objectAtIndex:i+1] objectAtIndex:j-1] &&
						 s == [[self.board objectAtIndex:i+2] objectAtIndex:j-2] &&
						 s == [[self.board objectAtIndex:i+3] objectAtIndex:j-3])
					{
						winner = s;
					}
				} @catch (NSException *moo) {}
			}
		}
	}
	printf("Winner is: %s\n", winner.side_name);
	return winner.side_name;
}


@end
